//Copyright (c) Microsoft Corporation. All rights reserved.
using System;

namespace Microsoft.Office.OneNote
{
	/// <summary>
	/// Represents errors that occur during import into OneNote.
	/// </summary>
	[Serializable]
	public class OneNoteException : Exception
	{
		/// <summary>
		/// Constructs a new OneNoteException.
		/// </summary>
		public OneNoteException()
		{
		}

		/// <summary>
		/// Constructs a new OneNoteException based off of the
		/// specified HResult.
		/// </summary>
		/// <param name="errorCode">
		/// The HRESULT returned by the ApplicationServer COM interface.
		/// </param>
		public OneNoteException(int errorCode)
		{
			HResult = errorCode;

			// Map the hResult to an error string:
			switch ((uint) errorCode)
			{
				case 0x80042000:
					// MALFORMED_XML_ERROR
					// Seeing this error indicates that something major
					// has gone really really wrong, particularly given
					// that we actually validate all XML before importing!
					message = "Error: Attempting to import malformed XML!";
					break;

				case 0x80042001:
					// INVALID_XML_ERROR
					// Ditto -- this should never be happening, but may
					// be occurring if our schema doesn't match the schema
					// being used by OneNote.
					message = "Error: Attempting to import invalid XML!";
					break;

				case 0x80042002:
					// ERROR_CREATING_SECTION
					// OneNote tried to create the section specified but 
					// failed -- the filepath may have been invalid, or
					// permission was denied, etc.
					message = "Unable to create the specified section.";
					break;

				case 0x80042003:
					// ERROR_OPENING_SECTION
					// The specified section could not be opened -- most
					// likely due to lack of file permissions.
					message = "Unable to open the specified section.";
					break;

				case 0x80042004:
					// SECTION_DOES_NOT_EXIST_ERROR
					// The section specified no longer exists... perhaps
					// the user deleted it after it was initially imported?
					// This is a fairly normal error when calling 
					// Page.NavigateTo(), but shouldn't be thrown via Page.Commit().
					message = "The specified section does not exist.";
					break;

				case 0x80042005:
					// PAGE_DOES_NOT_EXIST_ERROR
					// The section page no longer exists... perhaps
					// the user deleted it after it was initially imported?
					// This is a fairly normal error when calling 
					// Page.NavigateTo(), but shouldn't be thrown via Page.Commit().
					message = "The specified page does not exist.";
					break;

				case 0x80042006:
					// FILE_DOES_NOT_EXIST_ERROR
					// One of the FileData objects is referencing a path
					// that does not exist, or cannot be accessed due
					// to permission issues.
					message = "Unable to access the referenced file.";
					break;

				case 0x80042007:
					// ERROR_INSERTING_IMAGE
					// The specified image file or data is not a supported
					// image filetype.
					message = "Unrecognized image data.";
					break;

				case 0x80042008:
					// ERROR_INSERTING_INK
					// The specified ink file or data is not valid ISF.
					message = "Unrecognized ink data.";
					break;

				case 0x80042009:
					// ERROR_INSERTING_HTML
					// The specified html file could not be imported
					message = "Error import html content.";
					break;

				case 0x8004200a:
					// ERROR_NAVIGATING_TO_PAGE
					// An unknown error occurred while OneNote attempting to 
					// navigate to the specified page.
					message = "Unable to navigate to the specified page.";
					break;

				case 0x8004200b:
					// SECTION_IS_READ_ONLY
					// Attempted to make modifications to a section file that 
					// is read only.
					message = "Unable to modify read-only section.";
					break;

				case 0x8004200c:
					// PAGE_IS_READ_ONLY 
					// Attempted to make modifications to a page that is read
					// only.
					message = "Unable to modify read-only page.";
					break;

				case 0x8004200d:
					// ERROR_INSERTING_OUTLINETEXT 
					// The OutlineElement/text specified in the XML failed to 
					// import successfully.
					message = "Failed to import outline element.";
					break;

				case 0x8004200e:
					// PAGE_OBJECT_DOES_NOT_EXIST_ON_PAGE 
					// The PageObject that is specified does not exist on the 
					// page.
					message = "The specified page object does not exist.";
					break;

				case 0x8004200f:
					// BINARY_OBJECT_DOES_NOT_EXIST_ON_PAGE 
					// The Binary object that is specified does not exist on 
					// the page.
					message = "The specified binary object does not exist on the page.";
					break;

				case 0x80042010:
					// LAST_MODIFIED_DATE_DID_NOT_MATCH 
					// Returned by the following method if the dExpectedLastModified 
					// date/time was specified and didnt match that of the page.
					// Application::ImportChangesToPage
					// Application::DeletePage
					// Application::DeletePageObject
					message = "Unable to modify/delete page as last modified date didn't match";
					break;

				case 0x80042011:
					// GROUP_DOES_NOT_EXIST_IN_SECTION 0x80042011
					// Returned by the following method if the group specified is not in the section
					// Application::CreatePage
					message = "The specified page group does not exist";
					break;

				case 0x80042012:
					// PAGE_NOT_IN_SPECFIED_GROUP 0x80042012
					// Returned by the following method if the page specified 
					// is not part of the group specified
					// Application::CreatePage
					message = "Specified group doesn't contain the specified page";
					break;

				case 0x80042013:
					// HRESULT NO_ACTIVE_SELECTION_OR_IP
					// This method returns the HRESULT NO_WINDOW_OPEN 0x80042013 
					// if there is not a window open and hence no active selection.
					message = "OneNote isn't running or has no open windows";
					break;

				case 0x80042014:
					// OBJECT_DOES_NOT_XIST
					// Returned as an error when attempting to export a non-existent
					// hierarchy object, create a new hierarchy object in a non-existent
					// parent, or get/set the binary data for a non-existent object.
					message = "Object does not exist";
					break;

				case 0x80042015:
					// NOTEBOOK_DOES_NOT_EXIST
					// A notebook operation was attempted on a non-existent notebook.
					message = "Notebook does not exist.";
					break;

				case 0x80042016:
					// INSERTING_FILE
					// An error occurred while attempting to insert a file.
					message = "Error inserting file.";
					break;

				case 0x80042017:
					// INVALID_NAME
					// Thrown when renaming a folder or section with an invalid name.
					message = "Attempting to rename an entity with an invalid name.";
					break;

				case 0x80042018:
					// FOLDER_DOES_NOT_EXIST
					// The specified folder does not exist.
					message = "Folder does not exist.";
					break;

				case 0x80042019:
					// INVALID_QUERY
					// Unable to parse query text.
					message = "Unable to parse query text.";
					break;

				case 0x8004201a:
					// FILE_ALREADY_EXISTS
					// Attempting to publish overtop of an already existing file.
					message = "Destination file already exists.";
					break;

				case 0x8004201b:
					// SECTION_ENCRYPTED_AND_LOCKED
					// The specified page is encrypted and locked.  It must be
					// unlocked by the user before continuing.
					message = "The specified page is encrypted and locked.";
					break;

				case 0x8004201c:
					// DISABLED_BY_POLICY
					// This feature is disabled by administrator policy.
					message = "This feature is disabled by administrator policy.";
					break;

				case 0x8004201d:
					// NOT_YET_SYNCHRONIZED
					// The specified page/section/etc has not yet been replicated by OneNote.
					message = "The specified page/section has not yet been replicated.  Please try again later.";
					break;

				case 0x8004201e:
					// LEGACY_SECTION
					// The specified section is from OneNote 2003 and read only.
					message = "The specified section is from OneNote 2003 and cannot be modified.";
					break;

				default:
					// either COM Server crash or a new error we aren't aware about
					message = "Non-specified COM error: " + errorCode;
					break;
			}
		}

		/// <summary>
		/// Constructs a new OneNoteException with the specified message.
		/// </summary>
		/// <param name="message">The error message to be displayed to the user.</param>
		public OneNoteException(string message)
		{
			this.message = message;
		}

		/// <summary>
		/// Gets a message that describes the current exception.   
		/// </summary>
		public override string Message
		{
			get
			{
				return message;
			}
		}

		/// <summary>
		/// A message describing the current exception.
		/// </summary>
		private string message;
	}
}
